#pragma once
#include <vector>
#include <thread>

#include "poller.h"
#include "mutex_guard.h"

class EventLoop{
public:
    using TaskCallBack = std::function<void()>;
    EventLoop();
    ~EventLoop();

    void Loop();
    void RunInLoop(TaskCallBack cb);
    void QueueInLoop(TaskCallBack cb);

    void UpdateChannel(Channel* channel);
    void RemoveChannel(Channel* channel);

    void AssertInLoopThread();
    void Quit();
private:
    bool IsInLoopThread();
    void DoPendingFunctors();
    void Wakeup();
    void HandleRead();

private:
    std::thread::id threadID_;
    std::unique_ptr<Poller> poller_;
    int wakeupFd_;
    std::unique_ptr<Channel> wakeupChannel_;

    Mutex mutex_;
    std::vector<TaskCallBack> pendingFunctors_;
    bool callingPendingFunctors_;
};